package service;

import dao.MeetingDao;
import entity.Meeting;
import entity.PageEntity;

import java.util.List;

public class MeetingService {
    private MeetingDao meetingDao = new MeetingDao();

    /**
     * 会议搜索+分页
     * @param title
     * @param status
     * @param currPage
     * @return
     */
    public PageEntity<Meeting> findPage(String title, String status, Integer currPage) {
        //创建一个分页的实体
        PageEntity<Meeting>pageData = new PageEntity<>();
        Integer pageSize = pageData.getPageSize();

        //当前页的数据
        Integer startIndex = (currPage - 1) * pageSize;
        List<Meeting>rowData = meetingDao.findPage(startIndex,pageSize,title,status);

        //总记录数
        Integer totalCount = meetingDao.countByTitleAndStatus(title,status);

        //总页数
        Integer totalPage = totalCount % pageSize == 0 ? totalCount / pageSize : totalCount / pageSize + 1;

        pageData.setCurrPage(currPage);
        pageData.setData(rowData);
        pageData.setTotalCount(totalCount);
        pageData.setTotalPage(totalPage);

        return pageData;
    }

    /**
     * 发布会议
     * @param meeting
     */
    public void save(Meeting meeting) {
        meetingDao.save(meeting);

    }

    /**
     *查询会议详情
     * @return
     */
    public Meeting findById(Long meetingId) {
        return meetingDao.findById(meetingId);

    }

    /**
     *根据会议id查询该会议实到人数
     * @param meetingId
     * @return
     */
    public Integer countByMeetingId(Long meetingId) {
           return meetingDao.countByMeetingId(meetingId);
    }

    /**
     *判断指定用户是否参加指定会议
     * @param userId
     * @param meetingId
     * @return
     */
    public Boolean isMeetingJoin(Long userId, Long meetingId) {
        Integer count = meetingDao.countByUserIdAndMeetingId(userId,meetingId);
        return count > 0;
    }

    /**
     *参加会议
     * @param userId
     * @param meetingId
     */
    public void joinMeeting(Long userId, Long meetingId) {
              meetingDao.joinMeeting(userId,meetingId);
    }

    /**
     *取消会议
     * @param userId
     * @param meetingId
     */
    public void unJoinMeeting(Long userId, Long meetingId) {
            meetingDao.unJoinMeeting(userId,meetingId);
    }
}
